<?php

namespace Seller\Controller;

class IndexController extends BaseController {

    public function index() {
        //供应商品数量
        $gh_goods_total = M('goods_supply')->where(array('customer_id' => $this->__seller_info['id']))->count();
        $this->assign('gh_goods_total', $gh_goods_total);
        //分销商品数量
        $fx_goods_total = M('goods_distribution')->where(array('customer_id' => $this->__seller_info['id']))->count();
        $this->assign('fx_goods_total', $fx_goods_total);
        //分销订单总数
        $fx_order_total = M('order')->where(array('distribution_customer_id' => $this->__seller_info['id']))->count();
        $this->assign('fx_order_total', $fx_order_total);
        //分销订单总金额
        $fx_price_total = M('order')->where(array('distribution_customer_id' => $this->__seller_info['id']))->sum('amount_total');
        $this->assign('fx_price_total', number_format($fx_price_total, 2));
        //供应订单总金额
        $gh_price_info = M('order_line')->alias('oo')->join('__ORDER__ o ON o.id = oo.order_id')->where(array('oo.supply_customer_id' => $this->__seller_info['id'], 'oo.pay_status' => 1))->field('oo.supply_price,oo.num')->select();
        $gh_price_total = 0;
        //统计总金额
        if( $gh_price_info ) {
            foreach( $gh_price_info as $key => $value ) {
                $gh_price_total += $value['supply_price'] * $value['num'];
            }
        }
        
        //获得最新的供应信息
        $supply_list = M('goods_supply')->alias('gs')->join('__GOODS__ g ON g.id = gs.goods_id')->where(array('gs.customer_id' => $this->__seller_info['id']))->field('g.name,g.earn13,gs.*')->limit(8)->select();
        $this->assign( 'supply_list' , $supply_list );
        $this->assign('gh_price_total', number_format($gh_price_total, 2));
        //供应订单总数
        $gh_order_total = M('order_line')->alias('oo')->join('__ORDER__ o ON o.id = oo.order_id')->where(array('oo.supply_customer_id' => $this->__seller_info['id'], 'oo.pay_status' => 1))->getField('COUNT(distinct oo.order_id)');
        $this->assign('gh_order_total', $gh_order_total);
        //最新订单
        $order_list = M('Order')->alias('o')->join('__ORDER_LINE__ ol ON ol.order_id = o.id')->join('__CUSTOMER__ c ON c.id = ol.supply_customer_id')->join('LEFT JOIN __CUSTOMER_PLATFORM_BIND__ cpb ON cpb.id = o.platform_id')->where(array('distribution_customer_id' => $this->__seller_info['id']))
                        ->field('o.ref,o.amount_total,o.date_order,c.shop_name,cpb.name,o.pay_status,o.express_send_status,o.status')->order('o.id DESC')->group('ol.order_id')->limit(8)->select();
        $this->assign('order_list', $order_list);
        $this->assign('notice_total_goods', M('goods_supply')->where("customer_id = {$this->__seller_info['id']} AND stock_num < {$this->__goods_stock_warning}")->count());
        $this->assign('advent_total_goods' , M('GoodsDistribution')->where('`stock_num` > 0 AND `storage_end_time` >= "' . date('Y-m-d H:i:s') . '" AND `storage_end_time` < "' . date('Y-m-d H:i:s' , strtotime("+{$this->__goods_advent_warning} day")) . '" AND `customer_id` = ' . $this->__seller_info['id'])->count());
		$this->display();
    }

    public function add() {
        $this->display();
    }

    /**
     * 商家资料修改
     */
    public function saveInfo() {
        if (IS_POST) {
            $model = M('customer');
            if (isset($this->__post['poster']) && $this->__post['poster']) {
                $this->__post['poster']['id'] = $this->__seller_info['id'];
                if ($model->create($this->__post['poster']) === false) {
                    $this->error($model->getError());
                } else {
                    if ($model->save() === false) {
                        $this->error('数据修改失败，请重新再试！');
                    } else {
                        $_SESSION[$this->_soller_login_key] = $model->find($this->__seller_info['id']);
                        $this->success('商家资料更新成功');
                    }
                }
            }
            $this->error('参数不完整');
        } else {
            $this->display();
        }
    }

    /**
     * 商家密码修改
     */
    public function savePass() {
        if (IS_POST) {
            $model = M('customer');
            $save_data['password'] = isset($this->__post['new_pass']) && $this->__post['new_pass'] ? password($this->__post['new_pass']) : $this->error('新密码不能为空');
            $orgin_pass = isset($this->__post['orgin_pass']) && $this->__post['orgin_pass'] ? password($this->__post['orgin_pass']) : $this->error('原始面膜不能为空');
            if ($this->__seller_info['password'] != $orgin_pass) {
                $this->error('原始密码错误');
            }
            $save_data['id'] = $this->__seller_info['id'];
            if ($model->create($save_data) === false) {
                $this->error($model->getError());
            } else {
                if ($model->save() === false) {
                    $this->error('商家密码修改失败，请重新再试！');
                } else {
                    $_SESSION[$this->_soller_login_key] = '';
                    $this->success('商家密码修改成功', U('Public/login'));
                }
            }
        } else {
            $this->display();
        }
    }

    /**
     * 获取消息提示列表
     */
    public function systemMessageTips() {
        if (isset($this->__get['itemid']) && $this->__get['itemid']) {
            M('system_message')->where(array('target_id' => $this->__seller_info['id'], 'role' => 1, 'status' => 0, 'id' => $this->__get['itemid']))->save(array('status' => 1));
        }
        $message_total = $this->__sellerMessageTotal();
        $data['total'] = $message_total['message'];
        $data['list'] = M('system_message')->where(array('target_id' => $this->__seller_info['id'], 'role' => 1, 'status' => 0))->field('title,content,id,create_time AS time')->order('id DESC')->limit(3)->select();
        if ($data['total'] > 0) {
            foreach ($data['list'] as $key => $value) {
                $data['list'][$key]['time'] = formatTime($value['time']);
                $data['list'][$key]['url'] = U('Index/systemMessageShow', array('id' => $value['id']));
                $data['list'][$key]['content'] = mb_substr(strip_tags($value['content']), 0, 30, 'utf-8');
            }
        }
        $this->ajaxReturn($data);
    }

    /**
     * 获取消息提示列表
     */
    public function systemPromptTips() {
        if (isset($this->__get['itemid']) && $this->__get['itemid']) {
            M('system_prompt')->where(array('customer_id' => $this->__seller_info['id'], 'status' => 0, 'id' => $this->__get['itemid']))->save(array('status' => 1));
            $this->ajaxReturn(array('status' => 1));
        }
        $message_total = $this->__sellerMessageTotal();
        $data['total'] = $message_total['prompt'];
        $data['list'] = M('system_prompt')->where(array('customer_id' => $this->__seller_info['id'], 'status' => 0))->field('title,target_id,type,id,create_time AS time,id')->order('id DESC')->limit(3)->select();
        if ($data['total'] > 0) {
            foreach ($data['list'] as $key => $value) {
                switch ($value['type']) {
                    case '1' :
                    case '2' :
                        $data['list'][$key]['url'] = U('Order/detail', array('ref' => $value['target_id'], 'type' => ( $value['type'] == 1 ? 'fx' : 'gh' )));
                        break;
                }
                unset($data['list'][$key]['target_id']);
                unset($data['list'][$key]['type']);
                $data['list'][$key]['time'] = formatTime($value['time']);
            }
        }
        $this->ajaxReturn($data);
    }

    /**
     * 消息详情
     */
    public function systemMessageShow() {
        if (!isset($this->__get['id']) || !$this->__get['id'])
            exit('消息ID不能为空');
        $message_info = M('system_message')->where(array('target_id' => $this->__seller_info['id'], 'role' => 1, 'id' => $this->__get['id']))->find();
        if (!$message_info) {
            exit('消息信息不存在！');
        }
        M('system_message')->save(array('status' => 1, 'id' => $this->__get['id']));
        $this->assign('message_info', $message_info);
        $this->display();
    }

    /**
     * 获得消息提示数量
     */
    public function getMessageTotal() {
        $this->ajaxReturn($this->__sellerMessageTotal());
    }

    /**
     * 商家消息列表
     */
    public function systemMessageList() {
        $model = M('system_message');
        $list = $model->where(array('target_id' => $this->__seller_info['id'], 'role' => 1))->field('id,title,status,create_time')->page($this->__page, $this->__row)->order('id DESC')->select();
        $total = $model->where(array('target_id' => $this->__seller_info['id'], 'role' => 1))->count();
        $this->__assignPage($total);
        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]['create_time'] = formatTime($value['create_time']);
                $list[$key]['url'] = U('Index/systemMessageShow', array('id' => $value['id']));
            }
        }
        $this->assign('lists', $list);
        $this->display();
    }

    /**
     * 商家消息删除
     */
    public function systemMessageDelete() {
        if (!isset($this->__get['id']) || !$this->__get['id'])
            $this->error('消息ID不能为空');
        if (!$message_info = M('system_message')->where(array('target_id' => $this->__seller_info['id'], 'role' => 1, 'id' => $this->__get['id']))->find()) {
            $this->error('不能删除别人的信息');
        }
        if (M('system_message')->where(array('target_id' => $this->__seller_info['id'], 'role' => 1, 'id' => $this->__get['id']))->delete()) {
            $this->success('信息删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function uploadFile() {
        $upload = new \Think\Upload();
        $reslut = $upload->upload('', 'Image');
        if ($reslut === false) {
            $this->ajaxReturn(array('error' => $upload->getError(), 'status' => 400));
        } else {
            if ($reslut) {
                $returndata = array();
                foreach ($reslut as $key => $value) {
                    $returndata[] = array(
                        'path' => '/Uploads/' . $value['savepath'] . $value['savename'],
                        'code' => $value['sha1']
                    );
                }
                $this->ajaxReturn(array('msg' => '', 'code' => 200, 'data' => count($returndata) > 1 ? $returndata : $returndata[0]));
            }
        }
    }

}
